package com.qf.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.attendance.entity.Approve;
import com.qf.attendance.mapper.ApproveMapper;
import com.qf.attendance.qo.ApproveQO;
import com.qf.attendance.service.ApproveService;
import com.qf.attendance.vo.ApproveVO;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.utils.CommonBeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Service
public class ApproveServiceImpl implements ApproveService {

    @Resource
    private ApproveMapper approveMapper;

    /**
     * 查询
     *
     * @param timeSheetId
     * @return
     */
    @Override
    public RespResult<ApproveVO> select(int timeSheetId) {
        Approve approve = new Approve();
        ApproveVO approveVO = new ApproveVO();
        approve.setTimeSheetId(timeSheetId);
        QueryWrapper<Approve> wrapper = new QueryWrapper<>(approve);
        Approve approve1 = approveMapper.selectOne(wrapper);
        if (!ObjectUtils.isEmpty(approve1)) {
            CommonBeanUtils.copyProperties(approve1, approveVO);
            return RespResult.success(approveVO);
        }
        throw new RuntimeException("查询失败");
    }

    /**
     * 更新
     *
     * @param approveId
     * @param status
     * @return
     */
    @Override
    public RespResult update(int approveId, int status) {
        int count = approveMapper.update(approveId, status);
        if (count > 0) {
            return RespResult.success(null);
        }
        throw new RuntimeException("数据更新失败");
    }

    @Override
    public RespResult insert(ApproveQO approveQO) {
        Approve approve = new Approve();
        CommonBeanUtils.copyProperties(approveQO, approve);
        int count = approveMapper.insert(approve);
        if (count > 0) {
            return RespResult.success(null);
        }
        throw new RuntimeException("添加数据失败");
    }
}
